﻿Imports CapaLogica.CL.Foro
Imports CapaLogica.CL.Carrera
Imports CapaLogica.CL.UtilidadesGenerales
Imports CapaLogica
Imports CapaLogica.CL.Grupo

Public Class BuscarForos
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            cargarLista()
        End If

    End Sub

    Protected Sub cargarLista()
        cargarCarreras()
    End Sub

    Private Sub redirigirPorError()
        Response.Redirect("Inicio.aspx")
    End Sub

    Protected Sub cmbCarrera_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbCarrera.SelectedIndexChanged
        cargarCursos()
        cargarGrupos()
    End Sub



    Protected Sub cmbCurso_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbCurso.SelectedIndexChanged
        cargarGrupos()
    End Sub


    Public Sub getListaForos()
        Dim idCurso As Integer
        Dim idGrupo As Integer
        Dim idCarrera As Integer

        If (cmbCurso.SelectedValue = "" And cmbGrupo.SelectedValue = "") Then
            idCarrera = Nothing
            idCurso = Nothing
            idGrupo = Nothing
        ElseIf (cmbCurso.SelectedValue <> "" And cmbGrupo.SelectedValue = "") Then
            MsgBox("No hay grupos en ese curso, por lo tanto no hay foros")
            Exit Sub
            'idCarrera = cmbCarrera.SelectedValue()
            'idCurso = cmbCurso.SelectedValue()
            'idGrupo = Nothing
        End If

        Try
            Me.gridForo.DataSource = GestorForo.listarForos(idCarrera, idCurso, idGrupo)
            Me.gridForo.DataBind()
        Catch ex As Exception
            redirigirPorError()
        End Try
    End Sub


    Protected Sub btnBuscar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBuscar.Click
        getListaForos()
        refrescar()
    End Sub

    Public Sub refrescar()
        cmbCurso.DataSource = Nothing
        cmbGrupo.DataSource = Nothing
    End Sub

    Public Sub cargarCarreras()
        Try
            Me.cmbCarrera.DataSource = GestorCarrera.buscarCarrera("", "")
            Me.cmbCarrera.DataTextField = "nombre"
            Me.cmbCarrera.DataValueField = "id"
            Me.cmbCarrera.DataBind()
        Catch ex As Exception
            Throw New Exception("No hay carreras en la base de datos")
        End Try

    End Sub

    Public Sub cargarCursos()
        Try
            Me.cmbCurso.DataSource = GestorCurso.buscarCurso(Me.cmbCarrera.SelectedValue())
            Me.cmbCurso.DataTextField = "nombre"
            Me.cmbCurso.DataValueField = "id"
            Me.cmbCurso.DataBind()
        Catch ex As Exception
            Throw New Exception("No hay cursos en la base de datos")
        End Try

    End Sub

    Public Sub cargarGrupos()
        Try
            Me.cmbGrupo.DataSource = GestorGrupo.buscarGrupo(Me.cmbCarrera.SelectedValue(), Me.cmbCurso.SelectedValue())
            Me.cmbGrupo.DataTextField = "numGrupo"
            Me.cmbGrupo.DataValueField = "id"
            Me.cmbGrupo.DataBind()
        Catch ex As Exception
            Throw New Exception("No hay grupos en la base de datos")
        End Try
       
    End Sub

End Class